<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <el-col :lg="8" :md="12">
            <el-form-item label="游戏名称">
              <el-input
                :placeholder="'请输入游戏名称'"
                v-model="where.game_name"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :md="12">
            <el-form-item label="关联项目:">
              <PageSelect
                :placeholder="'请选择关联项目'"
                v-model="where.app_id"
                :whereName="'app_name'"
                :url="'/app/index'"
                :optionLable="'app_name'"
                :optionvalue="'id'"
                :multiple="false"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="8" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:game:add')"
            >添加
          </el-button>
          <!-- <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:game:dall')"
            >删除
          </el-button> -->
        </template>
        <!-- 操作列 -->
        <!-- <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="openEdit(row)"
            v-if="permission.includes('sys:game:edit')"
            >修改
          </el-link>
          <el-link
            type="primary"
            v-if="row.id == row.relation_game_id"
            :underline="false"
            @click="addgame(row)"
            >关联新增
          </el-link> -->
        <!-- <el-popconfirm
            class="ele-action"
            title="确定要删除此游戏吗？"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:game:delete')"
              >删除
            </el-link>
          </el-popconfirm> -->
        <!-- </template> -->

        <template slot="avatar" slot-scope="{ row }">
          <el-image
            style="width: 30px; height: 30px"
            :src="row.icon"
            fit="fit"
          ></el-image>
        </template>
        <template slot="action" slot-scope="{ row }">
          <el-dropdown>
            <el-button type="primary">
              操作列表<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="openEdit(row)"
                  v-if="permission.includes('sys:game:edit')"
                  >修改
                </el-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="openEditGameChargePoint(row)"
                  v-if="permission.includes('sys:app:edit')"
                  >计费点管理
                </el-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="openEditEngineeringPackage(row)"
                  v-if="permission.includes('sys:app:edit')"
                  >工程母包管理
                </el-link>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <GameEdit :data="current" :visible.sync="showEdit" @done="reload" />
    <GameChargePoint
      :data="current"
      :visible.sync="showEditGameChargePoint"
      :gameID="gameRowID"
      @done="reload"
    />
    <GameEngineeringPackage
      :data="current"
      :visible.sync="showEditEngineeringPackage"
      :gameID="gameRowID"
      @done="reload"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import GameEdit from "./game-edit";
import GameChargePoint from "./game-chargePoint";
import GameEngineeringPackage from "./game-engineeringPackage";
import PageSelect from "@/components/pageSelect/index.vue";

export default {
  name: "ExamGame",
  components: { GameEdit, PageSelect, GameChargePoint,GameEngineeringPackage },
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      // 表格数据接口
      url: "/game/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left"
        },
        {
          prop: "id",
          label: "游戏ID",
          width: 90,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: "app_name",
          label: "关联项目",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        // {
        //   prop: "app_id",
        //   label: "研发项目id",
        //   showOverflowTooltip: true,
        //   minWidth: 90,
        //   align: "center"
        // },

        {
          prop: "game_name",
          label: "游戏名称",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "yanfa_game_id",
          label: "研发游戏id",
          showOverflowTooltip: true,
          minWidth: 90,
          align: "center"
        },
        // {
        //   prop: 'icon',
        //   label: 'icon',
        //   showOverflowTooltip: true,
        //   align: 'center',
        //   minWidth: 200,
        //   slot: 'avatar'
        // },
        // {
        //   prop: 'sdk_version',
        //   label: '区别版本',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     if (cellValue == 1) {
        //       return '安卓'
        //     } else if (cellValue == 2) {
        //       return '苹果'
        //     }
        //   }
        // },
        // {
        //   prop: 'game_appid',
        //   label: '游戏appid',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center'
        // },
        // {
        //   prop: 'relation_game_id',
        //   label: '',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center'
        // },
        // {
        //   prop: 'relation_game_name',
        //   label: '游戏名称',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center'
        // },
        // {
        //   prop: 'pay_notify_url',
        //   label: '游戏支付通知地址',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center'
        // },
        // {
        //   prop: 'access_key',
        //   label: '访问秘钥',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center'
        // },
        // {
        //   prop: 'status',
        //   label: '状态',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     if (cellValue == 1) {
        //       return '启用'
        //     } else if (cellValue == 0) {
        //       return '禁用'
        //     }
        //   }
        // },
        {
          prop: "create_time",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: "action",
          label: "操作",
          width: 150,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right"
        }
        // {
        //   prop: 'update_time',
        //   label: '更新时间',
        //   sortable: 'custom',
        //   showOverflowTooltip: true,
        //   minWidth: 160,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     return this.$util.toDateString(cellValue)
        //   }
        // },
        // {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 150,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: 'right'
        // }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 当前行id数据
      gameRowID: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示计费点管理弹窗
      showEditGameChargePoint: false,
      // 是否显示工程母包管理弹窗
      showEditEngineeringPackage: false
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示编辑 */
    openEditGameChargePoint(row) {
      this.gameRowID = row.id;
      this.current = row;
      this.showEditGameChargePoint = true;
    },
    /* 显示编辑 */
    openEditEngineeringPackage(row) {
      console.log("!!rrr", row);
      this.gameRowID = row.id;
      this.current = row;
      this.showEditEngineeringPackage = true;
    },
    addgame(row) {
      this.$http.post("/Game/setRelation", { id: row.id }).then(res => {
        console.log(res);
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/game/delete", { id: row.id })
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的游戏吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/game/delete", { id: this.selection.map(d => d.id) })
            .then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    }
  }
};
</script>

<style scoped></style>
